﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace MegaMan
{
    class Player : AnimatedGameEntity
    {
        /// <summary>
        /// A imagem do tiro
        /// </summary>
        private Texture2D shotImage;
        public Texture2D ShotImage
        {
            get
            {
                return shotImage;
            }
            set
            {
                shotImage = value;
            }
        }

        private GameWindow window;

        /// <summary>
        /// A aceleracao
        /// </summary>
        private Vector2 acceleration = new Vector2(0.3f, 0.3f);
        public Vector2 Acceleration
        {
            get
            {
                return acceleration;
            }

            set
            {
                acceleration = value;
            }
        }

        private Vector2 speed = new Vector2(0, 0);

        private List<Shot> shotList;
        public List<Shot> ShotList
        {
            get
            {
                return this.shotList;
            }
        }

        private double lastShotTime;
        private double shotIntervalTime;

        private Animation animationStanding;

        public Player(Texture2D image, Texture2D shotImage, GameWindow window)
        {
            entityImage = image;
            this.shotImage = shotImage;
            this.window = window;
            this.shotList = new List<Shot>();
            this.lastShotTime = (double)-0.5;
            this.shotIntervalTime = (double)0.5;


            animationStanding = new Animation();

            animationStanding.FrameSize = new Vector2(45, 50);
            animationStanding.FrameTime = 250;
            animationStanding.FrameTotal = 4;

            EntitySourceRectangle = new Rectangle(3, 4, 45, 50);
            CurrentAnimation = animationStanding;
        }

        public override void Update(GameTime gameTime)
        {
            processInputs(gameTime);
            //entityMoveVectorBased(speed);
            processWindowsBounds();
            processShots(gameTime);
        }


        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(entityImage, entityPosition, null, Color.White, entityRotation, new Vector2(entityImage.Width / 2, entityImage.Height / 2), 1, SpriteEffects.None, 0);

            foreach (Shot shot in shotList)
            {
                shot.Draw(spriteBatch);
            }
        }

        // 
        // Método responsável pelo processamento das entradas(comandos).
        private void processInputs(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                //rotateEntityClockwise(0.05f);
                entityMoveNegativeY(3);
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                //rotateEntityCounterClockwise(0.05f);]
                entityMovePositiveY(3);
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                //rotateEntityClockwise(0.05f);
                entityMovePositiveX(3);
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                //rotateEntityCounterClockwise(0.05f);
                entityMoveNegativeX(3);
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                speed += new Vector2((float)Math.Cos(entityRotation), (float)Math.Sin(entityRotation)) * acceleration;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if (gameTime.TotalGameTime.TotalSeconds.CompareTo(lastShotTime + shotIntervalTime) > 0)
                {
                    shotList.Add(new Shot(window, shotImage, new Vector2(entityPosition.X + entityImage.Width / 2 * (float)Math.Cos(entityRotation), entityPosition.Y + entityImage.Height / 2 * (float)Math.Sin(entityRotation)), new Vector2((float)Math.Cos(entityRotation), (float)Math.Sin(entityRotation))));
                    lastShotTime = gameTime.TotalGameTime.TotalSeconds;
                }
            }
        }

        // Método responsável por verificar se o player saiu da tela e passar ele para o outro lado na posição correta.
        private void processWindowsBounds()
        {
            if (entityPosition.X >= window.ClientBounds.Width + entityImage.Width / 2)
            {
                entityPosition.X = -entityImage.Width / 2;
            }
            else if (entityPosition.X <= -entityImage.Width / 2)
            {
                entityPosition.X = window.ClientBounds.Width + entityImage.Width / 2;
            }
            else if (entityPosition.Y >= window.ClientBounds.Height + entityImage.Height / 2)
            {
                entityPosition.Y = -entityImage.Height / 2;
            }
            else if (entityPosition.Y <= -entityImage.Height / 2)
            {
                entityPosition.Y = window.ClientBounds.Height + entityImage.Height / 2;
            }
        }

        // Método responsável pelo processamento dos tiros do player.
        private void processShots(GameTime gameTime)
        {
            List<Shot> removedShots = new List<Shot>();
            foreach (Shot shot in shotList)
            {
                shot.Update(gameTime);

                if (!shot.isActive)
                {
                    removedShots.Add(shot);
                }
            }

            foreach (Shot shot in removedShots)
            {
                shotList.Remove(shot);
            }
        }
    }
}
